//
//  HomeViewController.m
//  LocalGoods2
//
//  Created by Feihong Ye on 2018/10/23.
//  Copyright © 2018年 Xiaodong Huang. All rights reserved.
//

#import "HomeViewController.h"
#import "Header.h"
#import <SDCycleScrollView.h>
#import "ClassificationView.h"
#import "HomeJPRecommendCell.h"// 精品推荐
#import "HomeBrandCell.h"//品牌故事
#import "HomePayListCell.h"//商品列表

#import "SearchView.h"// 搜索框

#import "ProductDetailsPageViewController.h"

static NSString *JCCell_id = @"JCCell_id";
static NSString *CHeaderView_id = @"CHeaderView_id";
static NSString *BrandCell_id = @"BrandCell_id";
static NSString *PayTableViewCell_id = @"PayTableViewCell_id";
@interface HomeViewController ()<ClassificationViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, HomePayListCellDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headerView;// 头部view
@property (nonatomic, strong) UIView *carouselView;
@property (nonatomic, strong) ClassificationView *classificationView;
@property (nonatomic, strong) SDCycleScrollView *cyckeScrollView;

@property (nonatomic, strong) UICollectionView *collectionView;// 精品推荐

@property (nonatomic, weak) SearchView *searchView;// 搜索框
@end

@implementation HomeViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_cyckeScrollView adjustWhenControllerViewWillAppera];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];

    
    SearchView *searchView = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, 240*Proportion_375, 40)];
    self.searchView = searchView;
    searchView.searchBar.delegate = self;
    self.navigationItem.titleView = searchView;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.estimatedRowHeight = 130;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:_tableView];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREAM_WEIGHT, 550*Proportion_375)];
    _headerView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.tableHeaderView = _headerView;
    
    [self.tableView registerClass:[HomeBrandCell class] forCellReuseIdentifier:BrandCell_id];
    [self.tableView registerClass:[HomePayListCell class] forCellReuseIdentifier:PayTableViewCell_id];
    [self makeUpUI];
}

- (void)makeUpUI {
    
    UIView *YYBgView = [[UIView alloc] init];
    YYBgView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:YYBgView];
    //投影
    YYBgView.layer.shadowColor = [UIColor blackColor].CGColor;
    YYBgView.layer.shadowOpacity = 0.3;
    YYBgView.layer.shadowRadius = 4;
    YYBgView.layer.shadowOffset = CGSizeMake(0, 4);
    
    self.carouselView = [[UIView alloc] init];
    _carouselView.backgroundColor = [UIColor whiteColor];
    [_headerView addSubview:_carouselView];
    [_carouselView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(283*Proportion_375);
    }];
    [YYBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(self.carouselView).offset(0);
        make.top.equalTo(self.carouselView).offset(100);
    }];
    
    self.cyckeScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:nil placeholderImage:image(@"banner加载失败与加载中")];
    self.cyckeScrollView.backgroundColor = [UIColor whiteColor];
    self.cyckeScrollView.localizationImageNamesGroup = @[@"玉器1", @"center2", @"center3"];
    _cyckeScrollView.autoScrollTimeInterval = 4.0;
    _cyckeScrollView.showPageControl = NO;
    _cyckeScrollView.pianYiNum = 6;
    [_carouselView addSubview:_cyckeScrollView];
    
    [_cyckeScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.offset(40);
        make.height.offset(150*Proportion_375);
    }];
    
    self.classificationView = [[ClassificationView alloc] init];
    self.classificationView.backgroundColor = [UIColor whiteColor];
    [_carouselView addSubview:_classificationView];
    [_classificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(self.cyckeScrollView.mas_bottom).offset(10);
    }];
    
    self.classificationView.delegate = self;
    
    // 精品推荐
    UIView *JPTJView = [[UIView alloc] init];
    JPTJView.backgroundColor = [UIColor whiteColor];
    [_headerView insertSubview:JPTJView atIndex:0];
    [JPTJView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(50*Proportion_375);
        make.top.equalTo(self.carouselView.mas_bottom);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"精品推荐";
    [JPTJView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.bottom.offset(-10);
    }];
    
    UIButton *moreButton = [[UIButton alloc] init];
    [moreButton setTitle:@"更多>>" forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor colorFromRed:204 green:204 bule:204] forState:UIControlStateNormal];
    moreButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [JPTJView addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(titleLabel);
    }];
    
    // 精品推荐
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置头部高度
    //flowLayout.headerReferenceSize = CGSizeMake(SCREAM_WEIGHT, 50);
    flowLayout.itemSize = CGSizeMake((SCREAM_WEIGHT-60)/3, (SCREAM_WEIGHT-60)/3);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.headerView insertSubview:_collectionView atIndex:0];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(JPTJView.mas_bottom).offset(0);
    }];
    
    [_collectionView registerClass:[HomeJPRecommendCell class] forCellWithReuseIdentifier:JCCell_id];
    // 注册头部
    //[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CHeaderView_id];
}

//collectionView Header
- (void)collectionViewHeaderView {
    
}

// MARK: - ClassificationViewDelegate
- (void)classificationViewWithClass:(ClassificationView *)view AndButtonTag:(UIButton *)sender {
    [self loadDate:sender.tag];
}

// MARK: - 数据刷新
- (void)loadDate:(NSInteger)tag {
    NSLog(@"数据刷新-%zd", tag);
}

// MARK: -  UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeJPRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JCCell_id forIndexPath:indexPath];
    cell.backgroundColor = [UIColor purpleColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductDetailsPageViewController * productVC = [[ProductDetailsPageViewController alloc] init];
    [self.navigationController pushViewController:productVC animated:YES];
}

/*
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:CHeaderView_id forIndexPath:indexPath];
        header.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"精品推荐";
        [header addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(20);
            make.bottom.offset(-10);
        }];
        
        UIButton *moreButton = [[UIButton alloc] init];
        [moreButton setTitle:@"更多>>" forState:UIControlStateNormal];
        [moreButton setTitleColor:[UIColor colorFromRed:204 green:204 bule:204] forState:UIControlStateNormal];
        moreButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [header addSubview:moreButton];
        [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-20);
            make.centerY.equalTo(titleLabel);
        }];
        
        return header;
    }
    
    return nil;
}
*/

// UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 搜索收起键盘
    UITextField *searchTextField = [self.searchView.searchBar valueForKey:@"_searchField"];
    searchTextField.text = @"";
    [self.searchView.searchBar resignFirstResponder];
    
    
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    return YES;
}
// MARK: - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HomeBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:BrandCell_id forIndexPath:indexPath];
        return cell;
    }
    HomePayListCell *cell = [tableView dequeueReusableCellWithIdentifier:PayTableViewCell_id forIndexPath:indexPath];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    }
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    
    UIView *view = [[UIView alloc] init];
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"为您优选";
    [view addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view);
        make.centerY.equalTo(view).offset(5);
    }];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    return footerView;
}

// MARK: - HomePayListCellDelegate 点击商品(id)
- (void)homePayListDidSelectCommodityID:(NSInteger)commodity_id {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
